﻿using System;
using UnityEngine;
using XLua;

/// <summary>
/// ui管理接口Ctrl
/// </summary>
public class IMediator
{
    /// <summary>
    /// Instantiate后立即启用 
    /// </summary>
    public virtual void Awake()
    {
        Panel = Go.GetComponent<UIPanel>();
        Tween = Go.GetComponentInChildren<UITweener>();
    }
    /// <summary>
    /// 每次打开完成时回调
    /// </summary>
    public virtual void Open()
    {
        IsOpen = true;
    }
    /// <summary>
    /// 每次关闭完成时回调
    /// </summary>
    public virtual void Close()
    {
        IsOpen = false;
    }
    /// <summary>
    /// 每次销毁完成时回调
    /// </summary>
    public virtual void Destroy()
    {
        Go = null;
        Mask = null;
        Panel = null;
        Tween = null;
    }
    /// <summary>
    /// 界面id
    /// </summary>
    public virtual int PanelId
    {
        get { return (int)PanelID.None; }
    }
    /// <summary>
    /// 界面打开还是关闭
    /// </summary>
    public bool IsOpen { get; private set; }
    /// <summary>
    /// 要管理的界面
    /// </summary>
    public GameObject Go { get; set; }
    /// <summary>
    /// 遮罩 没有为null
    /// </summary>
    public GameObject Mask { get; set; }
    /// <summary>
    /// 面板控件
    /// </summary>
    public UIPanel Panel { get; private set; }
    /// <summary>
    /// 预设上的动画Component（在go的子物体上）
    /// </summary>
    public UITweener Tween { get; private set; }

    public void SetLocalScale(Vector3 vector3)
    {
        Go.transform.localScale = vector3;
    }
}

public class CSIMediator : IMediator
{
    public override void Awake()
    {
        base.Awake();
        AddListener();
    }

    public override void Open()
    {
        base.Open();
        UpdateEvent();
    }

    public override void Close()
    {
        base.Close();
        UpdateEvent();
    }

    public override void Destroy()
    {
        RemoveListener();
        base.Destroy();
    }

    /// <summary>
    /// 设置界面初始参数（每次打开界面执行）
    /// </summary>
    public virtual void SetData(params object[] args) { }
    /// <summary>
    /// 界面数据刷新事件
    /// </summary>
    public virtual void UpdateEvent() { }
    /// <summary>
    /// 按钮点击事件添加
    /// </summary>
    public virtual void AddListener() { }
    /// <summary>
    /// 按钮点击事件移除
    /// </summary>
    public virtual void RemoveListener() { }
}

[CSharpCallLua]
public delegate void LuaAwake(object[] args);

public class LuaIMediator : IMediator
{
    protected string className;
    protected int panelId;

    private LuaTable mLuaTable;
    private LuaAwake mAwakeFunc;
    private Action mOpenFunc;
    private Action mCloseFunc;
    private Action mDestroyFunc;

    public LuaIMediator(string name, int id)
    {
        className = name;
        panelId = id;
    }

    private void InitAction()
    {
        mLuaTable = LuaMgr.GetSingleton().GetLuaTable(className);
        if (mLuaTable == null) { return; }

        mAwakeFunc = mLuaTable.Get<LuaAwake>("Awake");
        mOpenFunc = mLuaTable.Get<Action>("Open");
        mCloseFunc = mLuaTable.Get<Action>("Close");
        mDestroyFunc = mLuaTable.Get<Action>("Destroy");
    }

    private void ClearAction()
    {
        mAwakeFunc = null;
        mOpenFunc = null;
        mCloseFunc = null;
        mDestroyFunc = null;

        if (mLuaTable != null)
        {
            mLuaTable.Dispose();
            mLuaTable = null;
        }
    }

    public override void Awake()
    {
        base.Awake(); InitAction();
        object[] args = { Go, Mask };
        if (mAwakeFunc != null)
        {
            mAwakeFunc(args);
        }
    }

    public override void Open()
    {
        base.Open();
        if (mOpenFunc != null)
        {
            mOpenFunc();
        }
    }

    public override void Close()
    {
        base.Close();
        if (mCloseFunc != null)
        {
            mCloseFunc();
        }
    }

    public override void Destroy()
    {
        if (mDestroyFunc != null)
        {
            mDestroyFunc();
        }
        ClearAction();
        base.Destroy();
    }

    public override int PanelId
    {
        get { return panelId; }
    }
}